package com.run.motion.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.run.motion.config.IpConfig;
import com.run.motion.entity.Motion;
import com.run.motion.entity.User;
import com.run.motion.entity.vo.MotionRspVo;
import com.run.motion.entity.vo.MotionVo;
import com.run.motion.service.IMotionService;
import com.run.motion.service.UserService;
import com.run.motion.util.page.AjaxResult;
import com.run.motion.util.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

/**
 * 运动记录Controller
 * 
 * @author lyq
 * @date 2022-03-26
 */
@Api(tags = "运动记录")
@RestController
@RequestMapping("/motion")
public class MotionController extends BaseController
{
    @Autowired
    private IMotionService motionService;
    @Autowired
    private UserService userService;
    @Autowired
    private IpConfig ipConfig;

    /**
     * 查询运动记录列表
     */
    @ApiOperation(value = "查询运动记录列表")
    @GetMapping("/list")
    public TableDataInfo list(Motion motion)
    {
        startPage();
        List<Motion> list = motionService.selectMotionList(motion);
        return getDataTable(list);
    }

    /**
     * 查询公开数据的用户的运动记录列表
     */
    @ApiOperation(value = "查询公开数据的用户的运动记录列表")
    @ApiImplicitParam(name = "distance", value = "公里数", paramType = "query", dataType = "String")
    @GetMapping("/overt/list")
    public TableDataInfo overtList(@RequestParam("distance") Double distance)
    {
        startPage();
//        查出公开运动记录的用户
        List<User> userList = userService.selectUserListByOvert();
        System.out.println("userList"+userList.toString());
        System.out.println("userListSize"+userList.size());
        if (userList.size() == 0){
            return getDataTable(null);
        }
        List<String> array = new ArrayList<>();
        for (int i = 0; i < userList.size(); i++) {
            array.add(userList.get(i).getPid());
        }
//        根据用户记录去查找
        MotionVo motionVo = new MotionVo();
        motionVo.setPids(array);
        motionVo.setDistance(distance);
        List<Motion> list = motionService.selectMotionByPids(motionVo);
        System.out.println(list.toString());
//        list 大小为1 但是内容为空 下面是网上找到的解决方法
        list =  list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
//        拼接数据
        List<MotionRspVo> data = new ArrayList<>();
        for (int i = 0; i < userList.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if(userList.get(i).getPid().equals(list.get(j).getUserId())){
                    MotionRspVo rspVo = new MotionRspVo();
                    userList.get(i).setAvatar(ipConfig.getIp()+userList.get(i).getAvatar());
                    rspVo.setUserId(userList.get(i).getPid());
                    rspVo.setUser(userList.get(i));
                    rspVo.setMotion(list.get(j));
                    data.add(rspVo);
                }
            }
        }
        return getDataTable(data);
    }



    /**
     * 获取运动记录详细信息
     */
    @ApiOperation(value = "获取运动记录详细信息")
    @GetMapping(value = "/{moid}")
    public AjaxResult getInfo(@PathVariable("moid") Long moid)
    {
        return AjaxResult.success(motionService.selectMotionByMoid(moid));
    }

    /**
     * 新增运动记录
     */
    @ApiOperation(value = "新增运动记录")
    @PostMapping
    public AjaxResult add(@RequestBody Motion motion)
    {
        return toAjax(motionService.insertMotion(motion));
    }

    /**
     * 修改运动记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Motion motion)
    {
        return toAjax(motionService.updateMotion(motion));
    }

    /**
     * 删除运动记录
     */
	@DeleteMapping("/{moids}")
    public AjaxResult remove(@PathVariable Long[] moids)
    {
        return toAjax(motionService.deleteMotionByMoids(moids));
    }
}
